package org.chengyi.model_shop.service.impl;

import cn.hutool.json.JSONUtil;
import org.chengyi.model_shop.entity.User;
import org.chengyi.model_shop.mapper.PersonMapper;
import org.chengyi.model_shop.service.PersonService;
import org.chengyi.model_shop.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class PersonServiceImpl implements PersonService {
    @Autowired
    private PersonMapper personMapper;
    
    @Override
    public String getUserInfo() {
        // 使用userId获取用户信息
        Integer userId = ThreadLocalUtil.get();
        User user = personMapper.getById(userId);
        Map<String, Object> result = new HashMap<>();
        if (user != null) {
            result.put("success", true);
            result.put("data", user); // Hutool会自动转换对象为JSON
        } else {
            result.put("success", false);
            result.put("message", "用户不存在");
        }
        return JSONUtil.toJsonStr(result);
    }

    @Override
    public String updateUserInfo(User user) {
        try {
            int affectedRows = personMapper.update(user);
            Map<String, Object> result = new HashMap<>();
            result.put("success", affectedRows > 0);
            result.put("message", affectedRows > 0 ? "更新成功" : "更新失败，用户不存在");
            return JSONUtil.toJsonStr(result);
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "更新失败: " + e.getMessage());
            return JSONUtil.toJsonStr(result);
        }
    }
}
